/*
 * Copyright (c) 2016 zhangcm. All Rights Reserved.
 */
package com.deram.ctin.dependencies.rest.compoment;


import com.deram.ctin.common.consts.ErrorConsts;
import com.deram.ctin.common.consts.error.SystemErrorConsts;
import com.deram.ctin.common.error.BusinessException;
import com.google.common.collect.Maps;
import feign.RetryableException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MissingPathVariableException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;

import java.util.Map;

/**
 * @author zhangcm
 * @version 1.0, 2017/8/26
 */
@RestControllerAdvice
public class ControllerAdvice {

    private final Logger logger = LoggerFactory.getLogger(ControllerAdvice.class);

    @ExceptionHandler
    @ResponseStatus(value = HttpStatus.BAD_REQUEST)
    public Object exception(MissingServletRequestParameterException e){
        return createResult("缺少参数" + e.getParameterName() + "[" + e.getParameterType() + "]");
    }

    @ExceptionHandler
    @ResponseStatus(value = HttpStatus.BAD_REQUEST)
    public Object exception(MethodArgumentTypeMismatchException e){
        return createResult("参数" + e.getName() + "格式不正确");
    }

    @ExceptionHandler
    @ResponseStatus(value = HttpStatus.BAD_REQUEST)
    public Object exception(HttpRequestMethodNotSupportedException e){
        return createResult("不支持" + e.getMethod() + "方法");
    }

    @ExceptionHandler
    @ResponseStatus(value = HttpStatus.BAD_REQUEST)
    public Object exception(MissingPathVariableException e){
        return createResult("缺少参数" + e.getVariableName());
    }

    @ExceptionHandler
    @ResponseStatus(value = HttpStatus.BAD_REQUEST)
    public Object exception(RetryableException e){
        logger.error(e.getMessage(), e);
        return createResult("服务连接失败");
    }

    @ExceptionHandler
    @ResponseStatus(value = HttpStatus.BAD_REQUEST)
    public Object exception(BusinessException e){
        logger.error(e.getMessage(), e);
        return createResult(e.getCode().getCode(), e.getCode().getMessage());
    }

    @ExceptionHandler
    @ResponseStatus(value = HttpStatus.BAD_REQUEST)
    public Object exception(Exception e){
        logger.error(e.getMessage(), e);
        return createResult("服务异常，请稍后重试");
    }

    private Object createResult(String message){
        return createResult(SystemErrorConsts.ERROR.getCode(), message);
    }

    private Object createResult(Integer code, String message){
        Map<String, Object> result = Maps.newHashMap();
        result.put("code", code);
        result.put("message", message);
        return result;
    }
}
